package com.cardvalue.sys.activity;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.view.View;
import android.widget.AutoCompleteTextView;
import com.cardvalue.sys.R;
import com.cardvalue.sys.adapter.BankListAdapter;
import com.cardvalue.sys.base.BaseActivity;
import com.cardvalue.sys.code.CallBackCode;
import com.cardvalue.sys.entity.Bank;
import com.cardvalue.sys.entity.SelHasLeaseContract;
import com.cardvalue.sys.fragment.BankCardFragment;
import com.cardvalue.sys.fragment.BankCardPrivateFragment;
import com.cardvalue.sys.mode.UserService;
import com.cardvalue.sys.tool.DividerItemDecoration;
import com.cardvalue.sys.tool.MTextWatcher;
import com.cardvalue.sys.tool.Utiltools;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import io.realm.Realm;

/**
 * 获取银行列表
 */
public class ConfirmListActivity extends BaseActivity {
    @Bind(R.id.ed_search) AutoCompleteTextView   ed_search;
    @Bind(R.id.recyclerView) RecyclerView mRecyclerView;
    private BankListAdapter mBankAdapter;
    private UserService userService;
    private Realm realm;
    private String type;
    private String tagName;
    List<SelHasLeaseContract> mlist=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_list);
        ButterKnife.bind(this);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        realm = Realm.getDefaultInstance();
        userService = new UserService(this);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(ConfirmListActivity.this, LinearLayoutManager.VERTICAL));
        mBankAdapter=new BankListAdapter(ConfirmListActivity.this,mlist);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(ConfirmListActivity.this,LinearLayoutManager.VERTICAL));
        mRecyclerView.setAdapter(mBankAdapter);
        initData();
        ed_search.addTextChangedListener(new MTextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                if(s != null && s.length() != 0){
                    try {
                        JSONObject jsonObject=new JSONObject();
                        JSONObject jsonObject1=new JSONObject();
                        jsonObject.put("type","1");
                        jsonObject1.put("bankName",s.toString());
                        jsonObject.put("$regex",jsonObject1.toString());
                        userService.getBank(jsonObject.toString(), new UserService.GetBankCallback() {
                            @Override
                            public void success(Bank bank) {
                                mBankAdapter.update(bank.getResults());

                            }

                            @Override
                            public void fail(Bank bank) {

                            }

                            @Override
                            public void cancelDialog() {

                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }
        });

        mBankAdapter.setOnItemClickListenter(new BankListAdapter.OnItemClickListenter() {
            @Override
            public void OnItemClick(View v, int position) {
                SelHasLeaseContract item  =mlist.get(position);
                Message ms=new Message();
                Bundle bundle=new Bundle();
                if(tagName.equals("directDebitBankName")){
                    ms.what= CallBackCode.CALLBACK_MSG_GETBANK_PRIVATE;
                    bundle.putString("title",item.getTitle().toString());
                    bundle.putString("id",item.getId()+"");
                    Utiltools.printE("选择银行的标题和id===",item.getTitle().toString()+"====id-="+item.getId()+"");
                    ms.setData(bundle);
                    BankCardPrivateFragment.handler.sendMessage(ms);
                }else if(tagName.equals("secondaryBankName")){
                    ms.what= CallBackCode.CALLBACK_MSG_GETBANK_PRIVATE;
                    bundle.putString("title",item.getTitle().toString());
                    bundle.putString("id",item.getId()+"");
                    Utiltools.printE("选择银行的标题和id===",item.getTitle().toString()+"====id-="+item.getId()+"");
                    ms.setData(bundle);
                    BankCardFragment.handler.sendMessage(ms);
                }else if(tagName.equals("secondaryBankName1")){
                    ms.what= CallBackCode.CALLBACK_MSG_GETBANK_PUBLIC;
                    bundle.putString("title",item.getTitle().toString());
                    bundle.putString("id",item.getId()+"");
                    Utiltools.printE("选择银行的标题和id===",item.getTitle().toString()+"====id-="+item.getId()+"");
                    ms.setData(bundle);
                    BankCardFragment.handler.sendMessage(ms);
                }
                finish();
            }
        });
    }

    //接受传过来的值
    public void initData(){
        String text = getIntent().getStringExtra("title");
        type = getIntent().getStringExtra("type");
        tagName = getIntent().getStringExtra("tagName");
        ed_search.setText(text);
    }

}
